package hivi.vn.hivigold.adapters;

import hivi.vn.hivigold.R;
import hivi.vn.hivigold.objects.HistoryDetail;
import hivi.vn.hivigold.objects.HistoryMaster;
import hivi.vn.hivigold.utils.Formats;

import java.util.ArrayList;

import org.kobjects.util.Strings;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

public class HistoryAdapter extends BaseExpandableListAdapter {
	Context c;
	ArrayList<HistoryMaster> arrMaster;
	ArrayList<ArrayList<HistoryDetail>> arrDetail;

	public HistoryAdapter(Context c, ArrayList<HistoryMaster> arrMaster,
			ArrayList<ArrayList<HistoryDetail>> arrDetail) {
		// TODO Auto-generated constructor stub
		this.c = c;
		this.arrMaster = arrMaster;
		this.arrDetail = arrDetail;
	}

	@Override
	public Object getChild(int arg0, int arg1) {
		// TODO Auto-generated method stub
		return arrDetail.get(arg0).get(arg1);
	}

	@Override
	public long getChildId(int arg0, int arg1) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getChildView(int arg0, int arg1, boolean arg2, View arg3,
			ViewGroup arg4) {
		// TODO Auto-generated method stub
		Log.i("TEST", "CHILD1");
		View v = arg3;
		ViewDetail vd;
		if (v == null) {
			v = ((LayoutInflater) c
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE))
					.inflate(R.layout.history_detail_layout, null);
			vd = new ViewDetail();
			vd.lnlHeader = (LinearLayout) v.findViewById(R.id.lnlHeader);
			vd.txtMavt = (TextView) v.findViewById(R.id.txt_detail_mavt);
			vd.txtTenvt = (TextView) v.findViewById(R.id.txt_detail_tenvt);
			vd.txtSl = (TextView) v.findViewById(R.id.txt_detail_sl);
			vd.txtKho = (TextView) v.findViewById(R.id.txt_detail_quay);
			vd.txtTlgau = (TextView) v.findViewById(R.id.txt_detail_tlgau);
			vd.txtTlgda = (TextView) v.findViewById(R.id.txt_detail_tlgda);
			vd.txtHlgau = (TextView) v.findViewById(R.id.txt_detail_hlgau);
			vd.txtTongtlg = (TextView) v.findViewById(R.id.txt_detail_tongtlg);
			vd.txtTiencong = (TextView) v
					.findViewById(R.id.txt_detail_tiencong);
			vd.txtTienda = (TextView) v.findViewById(R.id.txt_detail_tienda);
			vd.txtGiavang = (TextView) v.findViewById(R.id.txt_detail_giavang);
			vd.txtGiaban = (TextView) v.findViewById(R.id.txt_detail_giaban);
			vd.txtThanhtien = (TextView) v
					.findViewById(R.id.txt_detail_thanhtien);
			v.setTag(vd);
		} else {
			vd = (ViewDetail) v.getTag();
		}
		if(arg1!=0)
		{
			vd.lnlHeader.setVisibility(View.GONE);
		}else
		{
			vd.lnlHeader.setVisibility(View.VISIBLE);
		}
		HistoryDetail detail =arrDetail.get(arg0).get(arg1); 
		vd.txtMavt.setText(detail.getFK_VTID());
		vd.txtTenvt.setText(detail.getFK_VTID()+"-"+detail.getVTNAME());
		vd.txtKho.setText(detail.getMA_KHO());
		vd.txtSl.setText(Formats.number(detail.getSO_LUONG()));
		vd.txtTlgau.setText(Formats.number(detail.getTLG_AU()));
		vd.txtTlgda.setText(Formats.number(detail.getTLG_DA()));
		vd.txtHlgau.setText(Formats.percent(detail.getHLG_AU()));
		vd.txtTongtlg.setText(Formats.number(detail.getTONG_TLG()));
		vd.txtTiencong.setText(Formats.money(detail.getTIEN_CONG()/1000));
		vd.txtTienda.setText(Formats.money(detail.getTIEN_DA()/1000));
		vd.txtGiavang.setText(Formats.money(detail.getGIA_AU()/1000));
		vd.txtGiaban.setText(Formats.money(detail.getGIA()/1000));
		vd.txtThanhtien.setText(Formats.money(detail.getTIEN()/1000));
		Log.i("TEST", "CHILD2");
		return v;
	}

	@Override
	public int getChildrenCount(int arg0) {
		// TODO Auto-generated method stub
		return arrDetail.get(arg0).size();
	}

	@Override
	public Object getGroup(int arg0) {
		// TODO Auto-generated method stub
		return arrMaster.get(arg0);
	}

	@Override
	public int getGroupCount() {
		// TODO Auto-generated method stub
		return arrMaster.size();
	}

	@Override
	public long getGroupId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getGroupView(int arg0, boolean arg1, View arg2, ViewGroup arg3) {
		// TODO Auto-generated method stub
		Log.i("TEST", "GROUP1");
		View v = arg2;
		ViewMaster vm;
		if(v==null)
		{
			v = ((LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.history_master_layout, null);
			vm = new ViewMaster();
			vm.txtNgayct = (TextView)v.findViewById(R.id.txt_master_ngayct);
			vm.txtSoct = (TextView)v.findViewById(R.id.txt_master_soct);
			vm.txtDiengiai = (TextView)v.findViewById(R.id.txt_master_diengiai);
			vm.txtTongtien = (TextView)v.findViewById(R.id.txt_master_t_tien);
			vm.txtNhanvien = (TextView)v.findViewById(R.id.txt_master_ma_nv);
			vm.txtThoigian = (TextView)v.findViewById(R.id.txt_master_thoigian);
			v.setTag(vm);
		}else
		{
			vm = (ViewMaster)v.getTag();
		}
		HistoryMaster master= arrMaster.get(arg0);
		
		vm.txtNgayct.setText(c.getResources().getString(R.string.lbl_voucher_ngayct)+""+Formats.ConvertDateFromDb(c, master.getNgay_ct()));
		vm.txtSoct.setText(c.getResources().getString(R.string.lbl_voucher_soct)+""+master.getSo_ct());
		vm.txtDiengiai.setText(c.getResources().getString(R.string.lbl_voucher_note)+""+master.getDien_giai());
		vm.txtTongtien.setText(c.getResources().getString(R.string.lbl_voucher_total)+""+Formats.money(master.getT_tien()/1000));
		vm.txtNhanvien.setText(c.getResources().getString(R.string.lbl_voucher_nhanvien)+""+master.getMa_nv());
		vm.txtThoigian.setText(c.getResources().getString(R.string.lbl_voucher_time)+""+master.getThoi_gian());
		Log.i("TEST", "GROUP2");
		return v;
	}

	static class ViewMaster {
		TextView txtSoct;
		TextView txtNgayct;
		TextView txtDiengiai;
		TextView txtTongtien;
		TextView txtNhanvien;
		TextView txtThoigian;
	}

	static class ViewDetail {
		LinearLayout lnlHeader;
		TextView txtMavt;
		TextView txtTenvt;
		TextView txtKho;
		TextView txtSl;
		TextView txtTlgau;
		TextView txtTlgda;
		TextView txtHlgau;
		TextView txtTongtlg;
		TextView txtTiencong;
		TextView txtTienda;
		TextView txtGiavang;
		TextView txtGiaban;
		TextView txtThanhtien;
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return false;
	}



	@Override
	public boolean isChildSelectable(int arg0, int arg1) {
		// TODO Auto-generated method stub
		return false;
	}
}
